module.exports = {
	OnMessageReceive
}

const fs = require('fs');
const { join } = require('path');
const plugins_path = join(rootpath, "plugins");

/** 严格匹配的指令 */
let MsgDic_Strict = {};
/** 前缀匹配的指令 */
let MsgDic_Prefix = [];
/** 后缀匹配的指令 */
let MsgDic_Suffix = [];

fs.readdirSync(plugins_path).forEach((item, index)=>{
	let fPath = join(plugins_path, item);
	let stat = fs.statSync(fPath);
	//单独的js文件直接执行
	if(stat.isFile() && item.endsWith(".js")){
		LoadPlugins(fPath);
	}else{
		//只执行文件夹里的index.js
		fPath = join(fPath, "index.js");
		if(fs.existsSync(fPath)){
			LoadPlugins(fPath);
		}
	}
});
MsgDic_Prefix.sort((opt1, opt2) => {
	return opt1.priority - opt2.priority;
});
MsgDic_Suffix.sort((opt1, opt2) => {
	return opt1.priority - opt2.priority;
});
console.log("-------------插件加载-------------");
console.log("关键词:");
for(let i in MsgDic_Strict){
	console.log(`name:${MsgDic_Strict[i].name},word:${MsgDic_Strict[i].word},method:${MsgDic_Strict[i].method}`);
}
console.log("前缀:");
for(let i in MsgDic_Prefix){
	console.log(`name:${MsgDic_Prefix[i].name},word:${MsgDic_Prefix[i].word},method:${MsgDic_Prefix[i].method}`);
}
console.log("后缀");
for(let i in MsgDic_Suffix){
	console.log(`name:${MsgDic_Suffix[i].name},word:${MsgDic_Suffix[i].word},method:${MsgDic_Suffix[i].method}`);
}
console.log("-------------加载结束-------------");

/** 加载Js插件 */
function LoadPlugins(file){
	try{
		const options = require(file); // 待加载的插件
		if(support.isArray(options)){
			options.forEach((item, index) => {
				load(item);
			});
		}else{
			load(options);
		}
	}catch (e){
		console.error(`插件加载失败:${file}\n${e}`)
	}

	function load(option){
		if(option?.enable === false) return;
		["name", "word", "method", "priority", "func"].forEach((item) => {
			if(option[item] === undefined){
				throw `插件缺乏属性:${item}`;
			}
		});
		try{
			switch (option.method.toLowerCase()){ // 转换成小写
				case "strict": // 严格匹配
					MsgDic_Strict[option.word] = option;
					break;
				case "prefix": // 前缀匹配
					MsgDic_Prefix.push(option);
					break;
				case "suffix": // 后缀匹配
					MsgDic_Suffix.push(option);
					break;
				default:
					console.log('\x1B[31m%s\x1B[0m',`插件加载失败:\"${option.name}\",原因:错误的匹配模式`);
			}
		}catch (e){
			console.log('\x1B[31m%s\x1B[0m',`插件加载失败:\"${file}\",原因:${e.message}`);
		}
	}
}

/** 消息事件 */
function OnMessageReceive(msg) {
	const str = msg.toString();
	let opt = MsgDic_Strict[str]; // 严格匹配
	if(opt){
		DealWith(msg, opt);
	}else{
		//查找前缀数组
		for(let i in MsgDic_Prefix){
			if(str.startsWith(MsgDic_Prefix[i].word)){
				opt = MsgDic_Prefix[i];
				break;
			}
		}
		//查找后缀数组
		for(let i in MsgDic_Suffix){
			if(str.endsWith(MsgDic_Suffix[i].word)){
				//与前缀数组里的取较小者,相同时前缀优先
				if(!opt || (opt && opt.priority > MsgDic_Suffix[i].priority)){
					opt = MsgDic_Suffix[i];
				}
				break;
			}
		}
		if(opt){
			DealWith(msg, opt);
		}
	}

}

/** 选择指定的插件处理消息 */
async function DealWith(msg, opt){
	console.log(`[${support.GetNowDate().format("YYYY-MM-DD hh:mm:ss")}]:执行插件:${opt.name}`);
	opt.func(msg);
}
